package com.ujs.edu.medicine.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.ujs.edu.medicine.dao.YunjiayiMessageMapper;
import com.ujs.edu.medicine.dao.YunjiayiNoticeMapper;
import com.ujs.edu.medicine.dao.YunjiayiUserMapper;
import com.ujs.edu.medicine.dataobject.YunjiayiMessage;
import com.ujs.edu.medicine.dataobject.YunjiayiNotice;
import com.ujs.edu.medicine.dataobject.YunjiayiUser;
import com.ujs.edu.medicine.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:
 * @Date: Created in 20:13 2020/8/29
 * Description: 通知控制类
 * Modified By:
 */
@RestController
public class NoticeController {

    @Autowired
    YunjiayiNoticeMapper yunjiayiNoticeMapper;
    @Autowired
    YunjiayiUserMapper yunjiayiUserMapper;
    @Autowired
    YunjiayiMessageMapper yunjiayiMessageMapper;
    @Autowired
    HttpServletRequest request;

    public String gettoken(){
        return request.getHeader("Authorization");
    }

    /*会诊消息列表,从医生列表发起会诊*/
    @RequestMapping(value = "/api/message/doctor",method = RequestMethod.POST)
    public Result putDoctorMessage(YunjiayiNotice notice){
        Result result=new Result();

        notice.setIsagree(0);
        int putdoc=yunjiayiNoticeMapper.insert(notice);
        if (putdoc==1){
            result.setMessage("发送成功");
            result.setCode(0);
            result.setData("等待医生回复");
        }else {
            result.setCode(1);
            result.setMessage("发送失败");
        }

        return result;
    }


   /* *//*应答会诊消息*//*
    @RequestMapping("/api/message/agree")
    public Result agreeMessage(@RequestParam("uId")Integer uId,@RequestParam("noticid")Integer noticId,@RequestParam("isAgree")Boolean isAgree){
        Result result=new Result();
        YunjiayiNotice notice=new YunjiayiNotice();
        notice.setNoticeId(noticId);

        if (isAgree==true){

            notice.setIsagree(1);
            int ag=yunjiayiNoticeMapper.updateByPrimaryKey(notice);
            if (ag==1){
                result.setCode(0);
                result.setMessage("成功");
            }else {
                result.setCode(1);
                result.setMessage("失败");
            }
        }else {
            notice.setIsagree(0);
            int ag=yunjiayiNoticeMapper.updateByPrimaryKey(notice);
            if (ag==1){
                result.setCode(0);
                result.setMessage("成功");
            }else {
                result.setCode(1);
                result.setMessage("失败");
            }
        }

        return result;
    }*/


  /*  *//*通知消息列表*//*
    @RequestMapping("/api/message/notice")
    public Result noticlist(@RequestParam("uId")Integer uId){
        Result result=new Result();

        List<YunjiayiNotice> notices=yunjiayiNoticeMapper.getNotic(uId);

        if (notices!=null){
            result.setCode(0);
            result.setMessage("查询成功");
            result.setData(notices);
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }

        return result;
    }*/

    /*获取具体聊天信息*/
    @RequestMapping("/api/message/detailmessage")
    public Result detailMessage(@RequestParam("noticeId")Integer noticId){
        Result result=new Result();

        YunjiayiNotice notice=yunjiayiNoticeMapper.selectByPrimaryKey(noticId);

        List<YunjiayiMessage> messages=yunjiayiMessageMapper.getMessage(notice.getFormuserid(),notice.getTouserid());
        if (messages!=null){
            result.setCode(0);
            result.setMessage("查询成功");
            result.setData(messages);
        }else {
            YunjiayiMessage message=new YunjiayiMessage();
            message.setFromuserid(notice.getFormuserid());
            message.setTouserid(notice.getTouserid());
            int mes=yunjiayiMessageMapper.insert(message);
            result.setCode(1);
            result.setMessage("创建聊天成功");
        }


        return result;
    }



    /*发送通知消息*/
    @RequestMapping(value = "/api/message/notice",method = RequestMethod.POST)
    public Result sendNoticeMessage(@RequestBody JSONObject object){
        Result result=new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid= JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登录");
            return result;
        }

        YunjiayiNotice notice=new YunjiayiNotice();

        notice.setContent(object.getString("content"));
        notice.setTouserid(object.getInteger("doctorID"));
        notice.setFormuserid(myuserid);

        int inserNotice=yunjiayiNoticeMapper.insert(notice);
        if (inserNotice!=0){
            result.setCode(0);
            result.setMessage("发送成功");
        }else {
            result.setCode(1);
            result.setMessage("发送失败");
        }

        return result;
    }
}
